package com.cos.hospital.ui.usercenter;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cos.hospital.R;
import com.cos.hospital.code.Code;
import com.cos.hospital.init.BaseActivity;
import com.cos.hospital.model.UHttp;
import com.cos.hospital.model.bean.ErrorBean;
import com.cos.hospital.model.bean.ResultBean;
import com.cos.hospital.model.bean.user.UserMsgBean;
import com.cos.hospital.model.connector.HttpMsgListener;
import com.cos.hospital.tool.SPUtil;
import com.cos.hospital.tool.UToast;
import com.cos.hospital.tool.UTxt;
import com.cos.hospital.ui.usercenter.adapter.MyMsgAdapter;
import com.cos.hospital.widget.MyDividerDecoration;
import com.cos.hospital.widget.ProgressDialog;
import com.google.gson.JsonObject;
import com.jude.utils.JUtils;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;


/**
 * @Description: 我的消息
 * @Author: Kosmos
 * @Date: 2018年09月14日 14:52
 * @Email: KosmoSakura@foxmail.com
 */
public class MyMsgActivity extends BaseActivity {
    @BindView(R.id.springView)
    SpringView spv;
    @BindView(R.id.rc)
    RecyclerView rc;
    private List<UserMsgBean.MsgBean> list;
    private BaseQuickAdapter adapter;
    private int index;
    private final int size = 6;

    @Override
    protected int layout() {
        return R.layout.activity_my_msg;
    }

    @Override
    protected void init() {
        setTitleLeftIcon();
        setTitleCenterTxt(getString(R.string.my_msg));

        list = new ArrayList<>();
        adapter = new MyMsgAdapter(list);
        adapter.openLoadAnimation();//开启默认动画AlphaInAnimation
        rc.setLayoutManager(new LinearLayoutManager(context));
        adapter.setEmptyView(R.layout.lay_empty, rc);//设置当list中元素个数为0时显示的布局
        rc.addItemDecoration(new MyDividerDecoration(Color.TRANSPARENT, JUtils.dip2px(1)));
        rc.setAdapter(adapter);
        spv.setHeader(new DefaultHeader(context));
        spv.setFooter(new DefaultFooter(context));
    }

    @OnClick({R.id.title_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
        }
    }

    /**
     * 1.消息内容以消息框形式展示，每页默认展示4条消息；
     * <p>
     * 2.消息内容默认展示全部字符内容，消息框固定大小，
     * 第一行左对齐显示消息标题；第二三行显示消息内容，0-50字符，
     * 当内容过多时，结尾以省略号标识；第
     * 四行，剧中显示消息日期；并且点击消息框后以新页面展示所有消息内容；
     * <p>
     * 3.消息框按照消息时间从上之下排列，最新消息位于最上方；
     * <p>
     * 4.消息框过多时，通过上滑加载更多消息；
     */
    @Override
    protected void logic() {
        spv.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                index = 1;
                getList();
                spv.onFinishFreshAndLoadDelay();
            }

            @Override
            public void onLoadmore() {
                index++;
                getList();
                spv.onFinishFreshAndLoadDelay();
            }
        });
        spv.onFinishFreshAndLoadDelay();
        adapter.setOnItemClickListener((adapter, view, position) -> {
            UserMsgBean.MsgBean bean = list.get(position);
            if (bean == null) {
                showNotice("数据异常");
                return;
            }
            Intent intent = new Intent(context, MyMsgDetailActivity.class);
            intent.putExtra("time", bean.getFldMessageTime());
//            intent.putExtra("title", bean.getTitle());
            intent.putExtra("msg", bean.getFldMessageContent());
            startActivity(intent);
        });
        index = 1;
        getList();
    }

    private void getList() {
        ProgressDialog.getInstance().startProgressDialog(this);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("fldReceiveId", SPUtil.getUserId());
        jsonObject.addProperty("pageIndex", index);
        jsonObject.addProperty("pageSize", size);
        UHttp.start(getServes().getMsgCenter(jsonObject), "我的消息", new HttpMsgListener<ResultBean<UserMsgBean>>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                addEvent(disposable);
            }

            @Override
            public void onError(String describe) {
                showNotice(describe);
            }

            @Override
            public void onFinish() {
                ProgressDialog.getInstance().stopProgressDialog();
            }

            @Override
            public void onSuccess(ResultBean<UserMsgBean> dto) {
                if (dto.isSuccess()) {
                    List<UserMsgBean.MsgBean> items = dto.getResult().getItems();
                    if (UTxt.isEmpty(items)) {
                        spv.setEnableFooter(false);
                        UToast.toast("没有更多了");
                    } else {
                        refresh(items);
                    }
                } else {
                    ErrorBean errorBean = dto.getError();
                    if (errorBean != null) {
                        showNotice(UTxt.isNull(errorBean.getMessage(), Code.DefaultPrompt));
                    } else {
                        showNotice(Code.DefaultPrompt);
                    }
                }
            }
        });
    }

    private void refresh(List<UserMsgBean.MsgBean> items) {
        if (index == 1) {
            list.clear();
            list.addAll(items);
            adapter.notifyDataSetChanged();
            spv.setEnableFooter(true);
        } else {
            int position = list.size();
            list.addAll(items);
            adapter.notifyDataSetChanged();
            rc.smoothScrollToPosition(position);
            spv.setEnableFooter(items.size() >= size);
        }
    }
}
